package com.global.college.views;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.global.college.CollegeApplication;
import com.global.college.R;
import com.global.college.data.inner.BannerBean;
import com.global.college.ui.banner.OnBannerClickListener;
import com.global.college.utils.CommonUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * TinyHung@Outlook.com
 * 2019/4/8
 * 带有手势缩放的 Banner
 * 无限循环原理：
 * 1：PagerAdapter 中的 getCount() 方法返回 data.size()+2，意在真实的数据加上头部和尾部，使其永远触达不到边界
 * 2：PagerAdapter 中的 instantiateItem 实例化ViewGroup时，
 * 应获取真实的的position,position=position%data.size()得到真实position
 * 3：PagerAdapter 中的 finishUpdate 方法中处理滚动结束后边界交换逻辑,详见 finishUpdate 方法注释
 * 4：如果关心onPageSelected事件，还需要注意获取真实的position
 */

public class TransformerVideoPager extends RelativeLayout {

    private List<BannerBean> mDataBeans;
    private ViewPager mViewPager;
    private TransformerViewpager mAdapter;

    OnSelectListener selectListener;

    private FixedSpeedScroller scroller;

    public TransformerVideoPager(Context context) {
        this(context, null);
    }

    @SuppressLint("WrongViewCast")
    public TransformerVideoPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.video_pager_transforme, this);
        int emptyHeight = CommonUtils.getStatusBarHeight(CollegeApplication.getContext()) + CommonUtils.dpToPxInt(CollegeApplication.getContext(), 20f);
        mViewPager = (ViewPager) findViewById(R.id.view_item_pager);
        //ViewPager的父容器高度确定
        RelativeLayout pagerLayout = (RelativeLayout) findViewById(R.id.re_item_pager_view);
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        int width = screenWidth * 7 / 10;
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) pagerLayout.getLayoutParams();
        layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        pagerLayout.setLayoutParams(layoutParams);
        //ViewPager宽度为父容器8/10，高度与父容器一致
        LayoutParams params = new LayoutParams(width, width * 9 / 16);
        mViewPager.setLayoutParams(params);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //注意这里也需要获取真实的position
                position %= mDataBeans.size();
                if (selectListener!=null){
                    selectListener.onSelect(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //设置ViewPager切换效果
        mViewPager.setPageTransformer(true, new TransformerPageAnimation());
    }

    public void changeItem(){
        int currentItem = mViewPager.getCurrentItem()+1;
        //如果已经滚动至第一个，则重新定位至data.size()+2-1的位置
      if (currentItem == (mDataBeans.size() + 2 - 1)) {
            currentItem = 1;
        }
        mViewPager.setCurrentItem(currentItem, true);
    }

    public void setScrollSpeed(){
        try {
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            scroller = new FixedSpeedScroller(mViewPager.getContext());
            scroller.setmDuration(800);
            field.set(mViewPager, scroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setCollegeTrans(boolean isCollege){
        mViewPager.setPageTransformer(true, new TransformerPageAnimation(isCollege));
    }

    public interface OnSelectListener {
        void onSelect(int position);
    }

    public void setSelectListener(OnSelectListener listener){
        this.selectListener = listener;
    }

    public void setLongPic(Context context) {
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        int width = (int) (screenWidth * 5.5f / 10f);
        mViewPager.setLayoutParams(new LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void initAdapter(OnBannerClickListener listener) {
        mAdapter = new TransformerViewpager(listener);
        mViewPager.setAdapter(mAdapter);
    }

    /**
     * 更新data
     *
     * @param data          源数据
     * @param fiexdPosition 默认显示位置
     */
    public void setDatas(List<BannerBean> data, int fiexdPosition) {
        if (null != mAdapter && null != mAdapter) {
            if (null != mDataBeans) {
                mDataBeans.clear();
            }
            if (null == mDataBeans) {
                mDataBeans = new ArrayList<>();
            }
            mDataBeans.addAll(data);
            //在finishUpdate中实现无限循环的时，需将此值设置为data.size()+2,
            //以免finishUpdate中设置了setCurrentItem之后ViewPager的Item不加载
            mViewPager.setOffscreenPageLimit(data.size() + 2);
            mAdapter.notifyDataSetChanged();
            if (data.size() > fiexdPosition) {
                mViewPager.setCurrentItem(fiexdPosition);
            }
        }

    }

    private class TransformerViewpager extends PagerAdapter {
        private OnBannerClickListener mListener;

        public TransformerViewpager(OnBannerClickListener listener) {
            mListener = listener;
        }

        /**
         * 此处返回数量为真实Item数量+2，头+尾，结合finishUpdate方法达到无限循环
         *
         * @return
         */


        @Override
        public int getCount() {
            return mDataBeans == null ? 0 : mDataBeans.size() + 2;
        }

        //每次调用了notifyDataSetChanged都将重绘
        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            position %= mDataBeans.size();
            BannerBean movieItem = mDataBeans.get(position);
            TransformerMoiveItem moiveItem = new TransformerMoiveItem(CollegeApplication.getContext());
            moiveItem.setData(movieItem, mListener);
            container.addView(moiveItem);
            return moiveItem;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (object instanceof TransformerMoiveItem) {
                TransformerMoiveItem moiveItem = (TransformerMoiveItem) object;
                moiveItem.onDestroy();
            }
            container.removeView((View) object);
        }

        /**
         * 已完全
         *
         * @param container
         */
        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
            int currentItem = mViewPager.getCurrentItem();
            //如果已经滚动至第一个，则重新定位至data.size()+2-1的位置
            if (currentItem == 0) {
                currentItem = mDataBeans.size();
                //如果已经滚动至data.size()+2-1，则重新定位至第一个位置，保留左侧一个
            } else if (currentItem == (mDataBeans.size() + 2 - 1)) {
                currentItem = 1;
            }
            mViewPager.setCurrentItem(currentItem, false);
        }
    }

    /**
     * 对应生命周期调用
     */
    public void onDestroy() {
        if (null != mDataBeans) {
            mDataBeans.clear();
        }
        if (null != mAdapter) {
            mAdapter.notifyDataSetChanged();
        }

        if (null != mViewPager) {
            mViewPager.removeAllViews();
            mViewPager = null;
        }
    }
}